package com.xxwy.ddu.cms.manager.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.xxwy.ddu.cms.authonrize.entity.Role;
import com.xxwy.ddu.cms.authonrize.mapper.RoleMapper;
import com.xxwy.ddu.cms.authonrize.service.IRoleService;
import com.xxwy.ddu.common.util.RUtil;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xxwy
 * on 2018/9/14 0014
 */
@RestController
@RequestMapping("roleInfo")
@Log
public class RoleManagerController {

    @Autowired
    IRoleService iRoleService;

    @GetMapping("all")
    public ResponseEntity getAllRole(@RequestParam int page,
                                     @RequestParam int limit) {
        Page<Role> rolePage = iRoleService.selectPage(new Page<>(page, limit));
        return RUtil.success(rolePage);
    }


    @DeleteMapping("role/{id}")
    public ResponseEntity deleteOne(@PathVariable int id) {
        iRoleService.delectUserWithRole(id);
        return RUtil.success();
    }

    @DeleteMapping("all")
    public ResponseEntity<ModelMap> deleteAll(@RequestBody List<Role> roles) {
        List<Integer> rIds = null;
        if (roles != null) {
            rIds = roles.stream().map(Role::getrId).collect(Collectors.toList());
        }
        if (rIds != null) {
            rIds.forEach(iRoleService::delectUserWithRole);
        }
        return RUtil.success();
    }

    @PostMapping("role")
    public ResponseEntity insertRoel(@RequestBody @Valid Role role) {
        role.setDeleteFlag(0);
        iRoleService.insert(role);
        return RUtil.success();
    }
}
